package com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.req.SmsWnsStudentLoanApplyExportReq;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.req.SmsWnsStudentLoanApplyReq;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.req.SmsWnsStudentLoanDetailReq;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.service.SmsWnsStudentLoanApplyService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.Future;

/**
 * <p>
 * 助学贷款申请表 前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2024-03-20
 */
@RestController
@SecretBody
@RequestMapping("/welcomeNewStudent/studentLoanApply")
public class SmsWnsStudentLoanApplyController {

    @Resource
    private SmsWnsStudentLoanApplyService smsWnsStudentLoanApplyService;
    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @ApiOperation("提交助学贷款申请")
    @PostMapping("/add")
    public ResponseResult addStudentLoanApply(@RequestBody SmsWnsStudentLoanApplyReq req, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsWnsStudentLoanApplyService.addStudentLoanApply(req, userId);
    }

    @ApiOperation("撤销申请")
    @PostMapping("/cancel")
    public ResponseResult cancelStudentLoanApply(@RequestBody SmsWnsStudentLoanApplyReq req, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsWnsStudentLoanApplyService.cancelStudentLoanApply(req, userId);
    }

    @ApiOperation("审批")
    @PostMapping("/allow")
    public ResponseResult allowStudentLoanApply(@RequestBody SmsWnsStudentLoanDetailReq req, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsWnsStudentLoanApplyService.allowStudentLoanApply(req, userId);
    }

    @ApiOperation("审批列表")
    @GetMapping("/list")
    public ResponseResult getStudentLoanApplyList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                  @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                  @RequestParam(value = "applyType") Integer applyType,
                                                  @RequestParam(value = "searchStr", required = false) String searchStr,
                                                  HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsWnsStudentLoanApplyService.getStudentLoanApplyList(pageNum, pageSize, applyType, searchStr, userId);
    }

    @ApiOperation("审批详情")
    @GetMapping("/detail")
    public ResponseResult getStudentLoanApplyDetail(@RequestParam(value = "id") String id, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsWnsStudentLoanApplyService.getStudentLoanApplyDetail(id, userId);
    }

    @ApiOperation("申请人指定流程(申请前获取)")
    @GetMapping("/processList")
    public ResponseResult getStudentLoanApplyOriginSettingProcess() {
        return smsWnsStudentLoanApplyService.getStudentLoanApplyOriginSettingProcess();
    }

    @ApiOperation(value = "查询所有申请记录")
    @GetMapping("/selectAll")
    public ResponseResult selectAllStudentLoanApply(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                    @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                    @RequestParam(value = "allowStatus", required = false) Integer allowStatus,
                                                    @RequestParam(value = "searchStr", required = false) String searchStr) {
        return smsWnsStudentLoanApplyService.selectAllStudentLoanApply(pageNum, pageSize, allowStatus, searchStr);
    }

    @ApiOperation("批量导出")
    @PostMapping("/batch/export")
    public ResponseResult exportStudentLoanApply(@RequestBody SmsWnsStudentLoanApplyExportReq req, HttpServletResponse response) {
        ResponseResult responseResult;
        try {
            Future<ResponseResult> future = smsWnsStudentLoanApplyService.exportStudentLoanApply(req, response);
            while (true) {
                if (future.isDone()) {
                    break;
                }
            }
            responseResult = future.get();
        } catch (Exception e) {
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;
    }

    @ApiOperation(value = "强制归档")
    @PostMapping("/forced")
    public ResponseResult forcedFinishStudentLoanApply(@RequestBody SmsWnsStudentLoanDetailReq req, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsWnsStudentLoanApplyService.forcedFinishStudentLoanApply(req, userId);
    }

    @ApiOperation(value = "强制流转")
    @PostMapping("/forcedTorsion")
    public ResponseResult forcedTorsionStudentLoanApply(@RequestBody SmsWnsStudentLoanDetailReq req) {
        return smsWnsStudentLoanApplyService.forcedTorsionStudentLoanApply(req);
    }

    @ApiOperation(value = "代审批")
    @PostMapping("/replaceApply")
    public ResponseResult replaceApplyStudentLoanApply(@RequestBody SmsWnsStudentLoanDetailReq req, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsWnsStudentLoanApplyService.replaceApplyStudentLoanApply(userId, req);
    }

    @ApiOperation(value = "强制驳回")
    @PostMapping("/forcedReject")
    public ResponseResult forcedRejectStudentLoanApply(@RequestBody SmsWnsStudentLoanDetailReq req, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsWnsStudentLoanApplyService.forcedRejectStudentLoanApply(req, userId);
    }

    @ApiOperation(value = "删除助学贷款申请")
    @PostMapping("/del")
    public ResponseResult deletedStudentLoanApply(@RequestParam("id") String id) {
        return smsWnsStudentLoanApplyService.deletedStudentLoanApply(id);
    }

}

